package com.sx.shopping.controller.goods;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sx.manage.controller.base.MBaseController;
import com.sx.manage.servlet.ViewAndModel;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;
import com.sx.model.shopping.mybatis.constants.type.GoodsDateType;
import com.sx.model.shopping.mybatis.form.goods.MGoodsInfoForm;
import com.sx.model.shopping.mybatis.vo.goods.MGoodsInfoVO;
import com.sx.shopping.service.goods.MGoodsRebateInfoWebService;



@Controller
@RequestMapping("/goods_rebate")
public class MGoodsRebateController extends MBaseController{
	/**
	 * 日志
	 */
	private Logger _log = LoggerFactory.getLogger(this.getClass());
	@Resource(name="goodsRebateInfoWebService")
	private MGoodsRebateInfoWebService goodsRebateInfoWebService ;
	
	/**
	   * 跳转到数据列表页
	   */
	  @RequestMapping(value="goodManageList")
	  public ViewAndModel pageIndex(){
		  ViewAndModel model=new ViewAndModel(request, cache );
		  				  model.setViewName("goodsManage/rebate/goodsRebateManageList");
	
		  return model;
	  }
	/**
	 *   保存
	 * @param form
	 * @return
	 */
	@RequestMapping(value="save")
	@ResponseBody
	public  ResultBooleanVO  save(MGoodsInfoForm  form ,BindingResult errors){
				
		return goodsRebateInfoWebService.save(form, this.loginConsumer());		
	}
	/**
	 *  保存并发布
	 * @param form
	 * @return
	 */
	@RequestMapping(value="saveAndIssue")
	@ResponseBody
	public  ResultBooleanVO  saveAndIssue(MGoodsInfoForm  form ,BindingResult errors){

		if (null != form.getPrice()) {
			Boolean b = is_num(String.valueOf(form.getPrice()));
			if (!b) {
				return new ResultBooleanVO(false, null, "金额格式不正确！");
			}
		}		
		_log.debug("save调用正常");
		return goodsRebateInfoWebService.saveAndIssue(form, this.loginConsumer());
	}
	/**
	   * 跳转到 添加界面
	   */
	  @RequestMapping(value="toAdd")
	  public ViewAndModel toAdd(){
		  ViewAndModel model=new ViewAndModel(request, cache );
		  				  model.setViewName("goodsManage/rebate/addRebateGoods");
	
		  return model;
	  }
	  /**
	   * 跳转到编辑界面
	   */
	  @RequestMapping(value="toEdit/{id}")
	  public ViewAndModel toEdit(@PathVariable Long id ){
		  ViewAndModel model=new ViewAndModel(request, cache );
		  				  model.setViewName("goodsManage/rebate/editRebateGoods");
		  ResultObjectVO<MGoodsInfoVO> result = goodsRebateInfoWebService.queryById(id);
		 		  		
		  model.addObject("goods",  result.getObjVo());
		  return model;
	  }
	  /**
	   * 跳转到详情界面
	   */
	  @RequestMapping(value="toView/{id}")
	  public ViewAndModel toView(@PathVariable Long id ){
		  ViewAndModel model=new ViewAndModel(request, cache );
		  				  model.setViewName("goodsManage/rebate/goodsRebateDetails");
		  ResultObjectVO<MGoodsInfoVO> result = goodsRebateInfoWebService.queryById(id);
		  		
		  model.addObject("goods",  result.getObjVo());
		  return model;
	  }
	/**
	 * 根据id查询
	 * @param id
	 * @return
	 */
	@RequestMapping(value="queryById")
	@ResponseBody
	public  ResultObjectVO<MGoodsInfoVO> queryById(Long id){
		if( id == null){
			return  new ResultObjectVO<>(false,null,"id没有传过来");
		}	
		return goodsRebateInfoWebService.queryById(id);
	}
	
	/**
	 * 查询分页信息
	 * @param id
	 * @return
	 */
	@RequestMapping(value="queryPage")
	@ResponseBody
	public ResultListVO<MGoodsInfoVO>  queryPage( MGoodsInfoForm  form ){ 
		_log.debug("商品queryPage调用正常");
		return this.goodsRebateInfoWebService.queryPage(form);
	}
	
	
	/** 6 商品上架 */ 
	/**
	 * 查询分页信息
	 * @param id
	 * @return
	 */
	@RequestMapping(value="onLine/{id}")
	@ResponseBody
	public ResultBooleanVO  onLine(@PathVariable Long id ){
		_log.debug("onLine调用正常");
		return this.goodsRebateInfoWebService.onLine( new Long[]{id},this.loginConsumer());
	} 
	
	/**
	 * 6.1 商品下架
	 * @param id
	 * @return
	 */
	@RequestMapping(value="offLine/{id}")
	@ResponseBody
	public ResultBooleanVO  offLine(@PathVariable Long id ){ 
		_log.debug("offLine调用正常");
		return this.goodsRebateInfoWebService.offLine( new Long[]{id},this.loginConsumer());
	} 		
	
	/**
	 * 7 查询列表信息
	 * @param id
	 * @return
	 */
	@RequestMapping(value="list")
	@ResponseBody
	public ResultListVO<MGoodsInfoVO>  list(MGoodsInfoForm  form){ 
		_log.debug("offLine调用正常");
		form.setData_type( GoodsDateType.DATA_COMMING_REBATE_PRODUCT);
		return this.goodsRebateInfoWebService.queryList(form);
	} 	
}
